package org.r.items.tally.consumer.security.userdetails;

import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.ImmutableSet;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.r.items.tally.consumer.dto.UserInfo;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

@Data
@NoArgsConstructor
public class TallyUserDetails implements UserDetails {

    private Long userid;
    private String username;
    private String password;
    private Boolean enabled;
    private Collection<SimpleGrantedAuthority> authorities;


    public TallyUserDetails(UserInfo userInfo){
        SimpleGrantedAuthority grantedAuthority = new SimpleGrantedAuthority(userInfo.getRuleCode());
        this.authorities = ImmutableSet.of(grantedAuthority);
        this.userid = userInfo.getUserId();
        this.username = userInfo.getUsername();
        this.password = userInfo.getPassword();
        this.enabled = ObjectUtil.equal(userInfo.getStatus(), 1);
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
